package com.icetec.iceintel.ui.activity;


import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.icetec.R;
import com.icetec.iceintel.database.SchemeControlSqlHelper;
import com.icetec.iceintel.framework.AnimatedExpandableListView;
import com.icetec.iceintel.framework.HorizontalListView;
import com.icetec.iceintel.framework.IceBaseActivity;
import com.icetec.iceintel.model.Scheme;
import com.icetec.iceintel.ui.adapter.SchemeSelectAdapter;
import com.icetec.iceintel.ui.adapter.SchemeTypeAdapter;
import com.icetec.iceintel.utility.ToastUtility;

/**
 * Created by amo on 2015/3/14.
 * 可扩展选择模式界面
 */
public class SchemeSelectActivity extends IceBaseActivity {


    AnimatedExpandableListView expandableListView;
    SchemeSelectAdapter schemeSelectAdapter;
    SchemeControlSqlHelper schemeControlSqlHelper;

    TextView backTv;
    TextView finishTv;
    EditText tileEdtext;
    HorizontalListView typeList;
    SchemeTypeAdapter typeAdapter;
    Scheme scheme;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置列表适配器IdeasExpandableListAdapter
        setContentView(R.layout.activity_addscheme);

        expandableListView=(AnimatedExpandableListView)findViewById(R.id.activity_addschemecontrol_lightlist_listview);

        backTv=(TextView)findViewById(R.id.activity_addschemecontrol_back);
        finishTv=(TextView)findViewById(R.id.activity_addschemecontrol_finish);
        tileEdtext=(EditText)findViewById(R.id.activity_addschemecontrol_modifyinfo_edtext);
        typeList = (HorizontalListView) findViewById(R.id.activity_addschemecontrol_type_lv);
        typeAdapter = new SchemeTypeAdapter(this);
        typeList.setAdapter(typeAdapter);

        typeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                typeAdapter.setChoice(position);
                typeAdapter.updateImage(view);
            }
        });

        schemeSelectAdapter=new SchemeSelectAdapter(this);

        schemeControlSqlHelper=new SchemeControlSqlHelper();

        expandableListView.setAdapter(schemeSelectAdapter);
        expandableListView.setGroupIndicator(null);
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

                ((CheckBox)v.findViewById(R.id.lightlist_item_group_checkbox)).setChecked(true);
                if (expandableListView.isGroupExpanded(groupPosition)) {
                    expandableListView.collapseGroupWithAnimation(groupPosition);
                } else {
                    expandableListView.expandGroupWithAnimation(groupPosition);
                }

                return true;
            }
        });


        backTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        finishTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              if(tileEdtext.getText().toString().equals("")){

                  ToastUtility.show(R.string.input_name);
              }

                else if(schemeSelectAdapter.getDealWithList().size()==0){

                  ToastUtility.show(R.string.must_choose_light);

              }


              else {
                  scheme = new Scheme();
                  scheme.setName(tileEdtext.getText().toString());
                  scheme.setType(typeAdapter.getChoiceType());
                  scheme.setDeviceArrayList(schemeSelectAdapter.getDealWithList());
                  if(schemeControlSqlHelper.addScheme(scheme)){

                      finish();

                  }

              }

            }
        });


    }


}





